

package cn.shengchao.examstar.distribute.infrastrure.service.impl;

import cn.shengchao.examstar.distribute.infrastrure.mapper.DistributeContentProductThirdMapper;
import cn.shengchao.examstar.distribute.infrastrure.po.DistributeContentProductThird;
import cn.shengchao.examstar.distribute.infrastrure.service.DistributeContentProductThirdService;
import cn.shengchao.examstar.rpc.base.PageResult;
import cn.shengchao.examstar.rpc.distribute.req.ThirdProductDistributeUserQueryPage;
import cn.shengchao.examstar.rpc.distribute.resp.ThirdProductDistributeForUserRespEntity;
import cn.shengchao.examstar.rpc.distribute.resp.ThirdProductDistributeProxyInfoRespEntity;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class DistributeContentProductThirdServiceImpl extends ServiceImpl<DistributeContentProductThirdMapper, DistributeContentProductThird> implements DistributeContentProductThirdService {
    @Override
    public PageResult<ThirdProductDistributeForUserRespEntity> pageList(ThirdProductDistributeUserQueryPage thirdProductDistributeUserQueryPage) {
        try(Page page= PageHelper.startPage(thirdProductDistributeUserQueryPage.getPage(),  thirdProductDistributeUserQueryPage.getSize())){
            List<ThirdProductDistributeForUserRespEntity> list=baseMapper.selectListByPageQuery(thirdProductDistributeUserQueryPage);
            return PageResult.of(page.getTotal(), list);
        }
    }
    @Transactional
    @Override
    public void saveUserNewSetting(DistributeContentProductThird distributeContentProductThird) {
        baseMapper.update(Wrappers.lambdaUpdate(DistributeContentProductThird.class)
                .eq(DistributeContentProductThird::getDataStatus,1)
                .eq(DistributeContentProductThird::getProductId,distributeContentProductThird.getProductId())
                .eq(DistributeContentProductThird::getUserId,distributeContentProductThird.getUserId())
                .set(DistributeContentProductThird::getValidEndTime,new Date())
                .set(DistributeContentProductThird::getDataStatus,2)
        );
        if (distributeContentProductThird.getValidStartTime()==null || distributeContentProductThird.getValidStartTime().getTime()<=new Date().getTime()){
            distributeContentProductThird.setValidStartTime(new Date());
        }
        baseMapper.insert(distributeContentProductThird);
    }

    @Override
    public BigDecimal sumPercentUseExcludeUserId(Long productId, Long userId) {
        return baseMapper.sumPercentUseExcludeUserId(productId,userId);
    }
    @Override
    public List<ThirdProductDistributeProxyInfoRespEntity> selectListByProductId(Long productId) {
        return baseMapper.selectListByProductId(productId);
    }
}
